using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SmartLink.Domain.Entity;
using SmartLink.IRepository;
using SmartLink.Repository.Base;
using Dapper;
using System.Data.Common;

namespace SmartLink.Repository
{
    public class PER_ActionPermissionsRepo : RepositoryBase<PER_ActionPermissions>, IPER_ActionPermissionsRepo
    {
        public IList<PER_ActionPermissions> GetActionPermissionsByUserId(int userId)
        {
            IList<PER_ActionPermissions> actionPermissions = new List<PER_ActionPermissions>();
            using (var conn = DapperHelper.GetDefaultConnection())
            {
                var sql = string.Format(@"select ap.* from PER_UserToRole utr inner join PER_ActionPermissionsRole apr 
                                          on utr.RID=apr.RID 
                                          inner join PER_ActionPermissions ap
                                          on apr.AID = ap.ID
                                          where utr.UserID =@UserId");
                actionPermissions = conn.Query<PER_ActionPermissions>(sql, new { UserId = userId }).ToList();
                conn.Close();

                return actionPermissions;
            }
        }
        /// <summary>
        ///获取所有的动作权限，主要是为了 
        /// </summary>
        /// <returns></returns>
        public IList<PER_ActionPermissions> GetAllActionPermissions()
        {
            IList<PER_ActionPermissions> actionPermissions = new List<PER_ActionPermissions>();
            using (var conn = DapperHelper.GetDefaultConnection())
            {
                var sql = string.Format(@"select * from  PER_ActionPermissions");
                actionPermissions = conn.Query<PER_ActionPermissions>(sql).ToList();
                conn.Close();
                return actionPermissions;
            }
        }


    }
}
